<!-- 广告位编辑弹窗 -->
<template>
  <el-dialog
    :title="isUpdate?'修改用户与账号':'添加用户与账号'"
    :visible="visible"
    width="460px"
    :destroy-on-close="true"
    :lock-scroll="false"
    @update:visible="updateVisible">
    <el-form
      ref="form"
      :model="form"
      :rules="rules"
      label-width="100px">
      <el-form-item label="组长:" prop="user_id">
        <el-select clearable  filterable v-model="form.user_id">
          <el-option v-for="item in config.user_list" :key="item.id" :label="item.realname" :value="item.id"/>
        </el-select>
      </el-form-item>
      <el-form-item label="组员:" prop="user_group_id">
        <el-select clearable  filterable v-model="form.user_group_id">
          <el-option v-for="item in config.user_list" :key="item.id" :label="item.realname" :value="item.id"/>
        </el-select>
      </el-form-item>
      <el-form-item v-if="config.auth_sku_all ===1" label="sku:" prop="platform_sku_select">
        <el-select filterable multiple v-model="form.platform_sku_select" :filter-method=getOrderSku>
          <el-option v-for="(item, index) in config.platform_sku_list" :key="index" :label="item" :value="item"/>
        </el-select>
      </el-form-item>
      <el-form-item v-else label="sku:" prop="platform_sku_select">
        <el-select filterable multiple v-model="form.platform_sku_select">
          <el-option v-for="(item, index) in config.platform_sku_list" :key="index" :label="item" :value="item"/>
        </el-select>
      </el-form-item>


<!--      <el-form-item label="账号:" prop="account_name">-->
<!--        <el-select clearable  filterable v-model="form.account_name">-->
<!--          <el-option v-for="item in config.account_list" :key="item.user_account" :label="item.platform_user_name" :value="item.user_account"/>-->
<!--        </el-select>-->
<!--      </el-form-item>-->
      <el-form-item label="状态:" prop="status">
        <el-radio-group
          v-model="form.status">
          <el-radio :label="1">启用</el-radio>
          <el-radio :label="2">停用</el-radio>
        </el-radio-group>
      </el-form-item>
    </el-form>
    <div slot="footer">
      <el-button @click="updateVisible(false)">取消</el-button>
      <el-button
        type="primary"
        @click="save"
        :loading="loading">保存
      </el-button>
    </div>
  </el-dialog>
</template>

<script>
import '@riophae/vue-treeselect/dist/vue-treeselect.css';

export default {
  name: 'UserAccountEdit',
  components: {},
  props: {
    // 弹窗是否打开
    visible: Boolean,
    // 修改回显的数据
    data: Object,
    configList: Object,
    // 栏目数据
    cateList: Array
  },
  data() {
    return {
      // 表单数据
      form: Object.assign({status: 1}, this.data),
      config: Object.assign({}, this.configList),
      // 表单验证规则
      rules: {
        user_id: [
          {required: true, message: '请选择用户', trigger: 'blur'}
        ],
        user_group_id: [
          {required: true, message: '请选择组员', trigger: 'blur'}
        ],
        // account_name: [
        //   {required: true, message: '请选择账号', trigger: 'blur'}
        // ],
        status: [
          {required: true, message: '请选择状态', trigger: 'blur'}
        ]
      },
      // 提交状态
      loading: false,
      // 是否是修改
      isUpdate: false
    };
  },
  watch: {
    data() {
      if (this.data) {
        this.form = Object.assign({}, this.data);
        this.isUpdate = true;
      } else {
        this.form = {};
        this.isUpdate = false;
      }
    },
    configList() {
      if (this.configList) {
        this.config = Object.assign({}, this.configList);
      } else {
        this.config = {};
      }
    }
  },
  methods: {
    /* 下拉树格式化 */
    normalizer(d) {
      return {
        id: d.id,
        label: d.name,
        children: d.children || undefined
      };
    },
    //获取订单sku
    getOrderSku(val)
    {
      if (val == '') return;
      this.$http.get('/giftSet/getOrderSku',{
        params: {
          sku: val
        }
      }).then(res => {
        if (res.data.code === 0) {
          this.config.platform_sku_list = res.data.data;
        } else {
          this.$message.error(res.data.msg);
        }
      }).catch(e => {
        this.$message.error(e.message);
      });
    },
    /* 保存编辑 */
    save() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          this.loading = true;
          this.$http.post('/userAccount/edit', this.form).then(res => {
            this.loading = false;
            if (res.data.code === 0) {
              this.$message.success(res.data.msg);
              if (!this.isUpdate) {
                this.form = {};
              }
              this.updateVisible(false);
              this.$emit('done');
            } else {
              this.$message.error(res.data.msg);
            }
          }).catch(e => {
            this.loading = false;
            this.$message.error(e.message);
          });
        } else {
          return false;
        }
      });
    },
    /* 更新visible */
    updateVisible(value) {
      this.$emit('update:visible', value);
    }
  }
}
</script>

<style scoped>
</style>
